package com.example.demo.mapper;

import com.example.demo.entity.Order;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface OrderMapper {

    @Insert("INSERT INTO order_table(user_id, product_name, quantity) VALUES(#{userId}, #{productName}, #{quantity})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insertOrder(Order order);

    @Select("SELECT * FROM order_table WHERE id = #{id}")
    Order getOrderById(int id);

    @Update("UPDATE order_table SET product_name = #{productName}, quantity = #{quantity} WHERE id = #{id}")
    int updateOrder(Order order);

    @Delete("DELETE FROM order_table WHERE id = #{id}")
    int deleteOrder(int id);

    @Select("SELECT * FROM order_table")
    List<Order> getAllOrders();

    @Select("SELECT o.id, o.user_id AS userId, o.product_name, o.quantity, u.name as user_name FROM order_table o JOIN user u ON o.user_id = u.id WHERE o.id = #{id}")
    Order getOrderWithUserDetails(int id);

    @Select("SELECT o.id, o.user_id AS userId, o.product_name AS productName, o.quantity, u.name AS userName FROM order_table o JOIN user u ON o.user_id = u.id WHERE o.user_id = #{userId}")
    List<Order> getOrdersByUserId(int userId);

    @Select("SELECT o.id, o.user_id AS userId, o.product_name AS productName, o.quantity, u.name AS userName " +
            "FROM order_table o JOIN user u ON o.user_id = u.id " +
            "WHERE u.name LIKE CONCAT('%', #{keyword}, '%')")
    List<Order> getOrdersByUserName(String keyword);

}
